<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$id = $_REQUEST["id"];
	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];
	$ex1_id = $_REQUEST["ex1_id"];
	$ex2_id = $_REQUEST["ex2_id"];
	$ex3_id = $_REQUEST["ex3_id"];
	$month_year = $_REQUEST["month_year"];
	$welfare = $_REQUEST["welfare"];

	$budget_year = $_REQUEST["budget_year"];

	$task = $_REQUEST["task"];

		/*
		$sql = "SELECT  MAX(running_id) as running_id FROM temp_running WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
		$db = new myDBS;
		$result = $db->runSQL($sql);
		$rows = $db->fetch_object($result);

		
		if (is_null($rows->running_id)) {
			$running = 0;
			$sql = "INSERT INTO temp_running (running_id, lvl1_id, budget_year) VALUES('1', '" .$_SESSION["lvl1_id"]. "', '$budget_year')";
			$db->insData($sql);
		}	else {
			$running = $rows->running_id;
			$sql = "UPDATE temp_running SET running_id = running_id + 1 WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
			$db->runSQL($sql);
		}
		$running = $running + 1;
		$running = $running ."-". $budget_year;
		*/

	$db = new myDBS;
	$db->selectDB("account");

	switch ($task) {
		case "new":
			$sql = "INSERT INTO tbl_welfare (
			id ,	panngan2_id ,	panngan3_id ,	ex1_id ,	ex2_id ,	ex3_id ,
			month_year ,	welfare
			)
			VALUES (
			 '$id',  '$panngan2',  '$panngan3',  '$ex1_id',  '$ex2_id',  '$ex3_id',  '$month_year',  '$welfare'
			)
			";

		//echo $sql;
		
			$db->insData($sql);
			break;
	case "edit":
		$sql = "UPDATE tbl_welfare SET welfare = '$welfare'
				WHERE  id = '$id'
				AND panngan2_id = '$panngan2'
				AND panngan3_id = '$panngan3'
				AND ex1_id = '$ex1_id'
				AND ex2_id = '$ex2_id'
				AND ex3_id = '$ex3_id'
				AND month_year = '$month_year'
		";
		$db->runSQL($sql);
		break;

		case "update_id":
			
			
			$sql = "SELECT  MAX(running_id) as running_id FROM temp_running WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
			//$db = new myDBS;
			$result = $db->runSQL($sql);
			$rows = $db->fetch_object($result);
			
			if (is_null($rows->running_id)) {
				$running = 0;
				$sql = "INSERT INTO temp_running (running_id, lvl1_id, budget_year) VALUES('1', '" .$_SESSION["lvl1_id"]. "', '$budget_year')";
				$db->insData($sql);
			}	else {
				$running = $rows->running_id;
				$sql = "UPDATE temp_running SET running_id = running_id + 1 WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
				$db->runSQL($sql);
			}
			$running = $running + 1;
			$running = $running ."-". $budget_year;

			
			$sql = "UPDATE tbl_welfare SET pet_id = '$running'
						WHERE panngan2_id = '$panngan2'
						AND panngan3_id = '$panngan3'
						AND ex1_id = '$ex1_id'
						AND ex2_id = '$ex2_id'
						AND ex3_id = '$ex3_id'
						AND month_year = '$month_year'
					";
			$db->runSQL($sql);
			
			echo "<input type='hidden' name='pet_id' value ='$running'>";

			break;

	}

	//echo $task;
/*
	if ($task == "edit") {

		$sql ="UPDATE tbl_petition SET inpValues = '$inpValues', inpTax = '$inpTax', inpTotal = '$inpTotal', WithholdingTax = '$WithholdingTax', inpPenalty ='$inpPenalty', inpNet = '$inpNet', comment = '$comment' WHERE petition_id = '$petition_id'";

		//echo $sql;
		$db = new myDBS;
		$db->runSQL($sql);
		echo $petition_id;
	} else if ($task == "cancel") {
		
		$sql ="UPDATE tbl_petition SET veri_status = 'Cancel', reason = '$reason', veri_date = NOW() WHERE petition_id = '$petition_id'";
		$db = new myDBS;
		$db->runSQL($sql);
		
	} else if ($task == "verify") {
		$sql ="UPDATE tbl_petition SET veri_status = 'Pass', veri_date = NOW() WHERE petition_id = '$petition_id'";
		$db = new myDBS;
		$db->runSQL($sql);

	} else if ($task == "approved") {
		$sql ="UPDATE tbl_petition SET approved_status = 'Approved', approved_date = NOW()  WHERE petition_id = '$petition_id'";
		$db = new myDBS;
		$db->runSQL($sql);

	} else if ($task == "disapproval") {
		$sql ="UPDATE tbl_petition SET approved_status = 'Disapproval', approved_date = NOW() WHERE petition_id = '$petition_id'";
		$db = new myDBS;
		$db->runSQL($sql);

	} else if ($task == "approvedCheque") {
		
		$sql ="UPDATE tbl_petition SET cheque_status = 'Approved', cheque_date = '$i_date' WHERE cheque_no = '$cheque_no'";
		//echo $sql;
		$db = new myDBS;
		$db->runSQL($sql);
		

	} else if ($task == "disapprovalCheque") {
		
		$sql ="UPDATE tbl_petition SET cheque_status = 'Disapproval' WHERE cheque_no = '$cheque_no'";
		//echo $sql;
		$db = new myDBS;
		$db->runSQL($sql);
		
	} else if ($task == "print") {
		//$val = explode(",", $val);

		$sql ="UPDATE tbl_petition SET cheque_no = '$cheque_no', branch_name = '$branch_name', bank_name = '$bank_name' WHERE company_name = '$company_name' and approved_status = 'Approved' and petition_id in($val)";
		//echo $sql;

		//echo $val;
		$db = new myDBS;
		$db->runSQL($sql); 
	
	} else if ($task == "paid") {
		
		$sql ="UPDATE tbl_petition SET paid_status = 'Paid', paid_date = NOW() WHERE cheque_no = '$cheque_no'";
		//echo $sql;
		$db = new myDBS;
		$db->runSQL($sql);

	} else if ($task == "new") {
		$sql = "SELECT  MAX(running_id) as running_id FROM temp_running WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
		$db = new myDBS;
		$result = $db->runSQL($sql);
		$rows = $db->fetch_object($result);

		if (is_null($rows->running_id)) {
			$running = 0;
			$sql = "INSERT INTO temp_running (running_id, lvl1_id, budget_year) VALUES('1', '" .$_SESSION["lvl1_id"]. "', '$budget_year')";
			$db->insData($sql);
		}	else {
			$running = $rows->running_id;
			$sql = "UPDATE temp_running SET running_id = running_id + 1 WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and budget_year = '$budget_year'";
			$db->runSQL($sql);
		}
		$running = $running + 1;
		$running = $running ."-". $budget_year;

		$sql ="INSERT INTO tbl_petition(ppID, lvl1_name, lvl2_name, budget_year, p_id, treasury_id, treasury_date, panngan2_name, panngan3_name, acc_id, elass, ex1_id, ex2_id, ex3_id, inpValues, inpTax, inpTotal, WithholdingTax, inpPenalty, inpNet, payfor_id, company_name, comment) VALUES('$ppID', '$lvl1_name', '$lvl2_name', '$budget_year', '$running', '$treasury_id', '$treasury_date', '$panngan2_name', '$panngan3_name', '$acc_id', '$elass', '$ex1_id', '$ex2_id', '$ex3_id', '$inpValues', '$inpTax', '$inpTotal', '$WithholdingTax', '$inpPenalty', '$inpNet', '$payfor_id', '$company_name', '$comment')";

		//echo $sql;
		//$db = new myDBS;
		$last_id = $db->insData($sql);
		
		//echo $sql;
		//$sql = "UPDATE pp_check_job SET status = 1 WHERE ppID = '$ppID'";
		//$db->runSQL($sql);

		


		echo $last_id;
	}

	//echo $sql;
	/*
	$db = new myDBS;
	$last_id = $db->insData($sql);
	echo $last_id;

	$where = "xxx_id = '$payfor_id' and creditor_name IS NOT NULL";
	//echo $where;
	$result = $db->showData1("temp_creditor", $where);

	while ( $rows = $db->fetch_object ( $result )){
		$sql = "INSERT INTO tbl_payfor(payfor_id, creditor_name, amount) VALUES('$payfor_id', '". $rows->creditor_name ."', '" . $rows->amount ."')";
		//echo $sql;
		$db->insData($sql);
	}
	*/
/*
	if (isset ($name)) {
		mysql_query("INSERT INTO tblProfile(name, email, tel, birthdate) VALUES ('$name', '$email', '$tel', '$datepicker')") or die(mysql_error($connect));
		echo "seccess";
	} else {
		//mysql_error($connect);
		echo "Error from filename:addProfile.php";
	}
*/
?>
